package com.fau.hm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalLeaveApprove;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 休假管理
 * @author dangjh
 * @datetime 2014年10月26日,上午11:30:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class VacationApproveAction extends PublicAction implements ModelDriven<MedicalLeaveApprove> {

	private static final long serialVersionUID = 1713700405822813176L;

	private MedicalLeaveApprove leave = new MedicalLeaveApprove();
	
	/**
	 * @desc 休假已通过信息列表-个人
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:44:28
	 */
	@Action(value = "init_leave_approve_list", results = {@Result(location = "init_leave_approve_list.jsp")})
	public String initLeaveApproveList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 休假已通过数据-个人
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:48:09
	 */
	@Action(value = "leave_approve_view")
	public String leaveApproveView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getLeaveService().findApproveLeave(map, leave, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 休假已通过信息列表-企业所有人
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:44:28
	 */
	@Action(value = "init_leave_approve_all_list", results = {@Result(location = "init_leave_approve_all_list.jsp")})
	public String initLeaveApproveAllList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 休假已通过数据-企业所有人
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:48:09
	 */
	@Action(value = "leave_approve_all_view")
	public String leaveApproveAllView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getLeaveService().findLeaveLogAll(map, leave, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 批准通过
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月26日,下午12:58:38
	 */
	@Action(value = "leave_on_pass")
	public String leaveOnPass(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(leave.getInfoId())){
			this.baseAllService.getLeaveService().saveApproveLeave(leave);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalLeaveApprove getModel() {
		return leave;
	}

}
